import CryptoJS from 'crypto-js'

export function objectToQueryString(obj) {
    return Object.keys(obj)
        .map(key => encodeURIComponent(key) + '=' + encodeURIComponent(obj[key]))
        .join('&');
}

// 根据数量返回1K+，2K+，3K+ ...
export function getNum(num) {
    if (num < 1000) {
        return num
    } else if (num >= 1000 && num < 10000) {
        return num / 1000 + 'K+'
    } else if (num >= 10000 && num < 100000) {
        return num / 10000 + 'W+'
    } else if (num >= 100000) {
        return num / 100000 + 'W+'
    }
}

// 0-50评分转换成4.0,4.5,4.8,5.0的数值
export function getScore(score) {
    if (score === 0) {
        return 0
    }
    if (score % 10 === 0) {
        return score / 10 + '.0'
    }
    return score / 10
}

// 数字补零 5=>5.0
export function addZero(num) {
    if (num >= 10) return num
    return '0' + num
}

// 生成一个不重复的随机数
export function getRandomNum(min = 3, max = 7) {
    return Math.floor(Math.random() * (max - min + 1) + min)
}

// 格式化日期字符串返回YYYY-MM-DD HH:mm:ss
export function formatDate(dateStr) {
    if (!dateStr) return '';

    try {
        const date = new Date(dateStr);
        if (isNaN(date.getTime())) {
            return '';
        }

        const year = date.getFullYear();
        const month = date.getMonth() + 1;
        const day = date.getDate();
        const hour = date.getHours();
        const minute = date.getMinutes();
        const second = date.getSeconds();

        return `${year}-${addZero(month)}-${addZero(day)} ${addZero(hour)}:${addZero(minute)}:${addZero(second)}`;
    } catch (error) {
        console.error('Date formatting error:', error);
        return '';
    }
}

// 日期格式化，当天只显示时分，否者当年显示月日， 否则显示年月日
export function formatDate2(dateStr) {
    const date = new Date(dateStr)
    const now = new Date()
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    // 当天-补零
    if (year === now.getFullYear() && month === now.getMonth() + 1 && day === now.getDate()) {
        return `${addZero(date.getHours())}:${addZero(date.getMinutes())}`
    }
    // 当年
    if (year === now.getFullYear()) {
        return `${addZero(month)}-${addZero(day)}`
    }
    return `${year}-${addZero(month)}-${addZero(day)}`
}

// 文件大小转换,不足1M显示KB, 不足1KB显示B, 不足1G显示MB，否则显示GB
export function fileSize(size) {
    if (size < 1024) {
        return size + ' B'
    }
    if (size < 1024 * 1024) {
        return (size / 1024).toFixed(2) + ' KB'
    }
    if (size < 1024 * 1024 * 1024) {
        return (size / 1024 / 1024).toFixed(2) + ' MB'
    }
    return (size / 1024 / 1024 / 1024).toFixed(2) + ' GB'
}

// 信用等级转换
export function getCreditLevel(score) {
    // 信用等级（0:普通 1:较好 2:优秀 3:极好 4: 完美 ）
    switch (score) {
        case 0:
            return '普通'
        case 1:
            return '较好'
        case 2:
            return '优秀'
        case 3:
            return '极好'
        case 4:
            return '完美'
        default:
            return '普通'
    }
}

// 数量单位转换，超过1000显示1K+，超过10000显示1W+，超过100000显示10W+
export function getNumUnit(num) {
    if (num >= 10000) return parseInt(num / 10000) + 'W+'
    if (num >= 1000) return parseInt(num / 1000) + 'K+'
    return num
}

// 登录加密
export function encrypt(loginParams) {
    const key = CryptoJS.enc.Utf8.parse('monkey1234567890');
    const iv = CryptoJS.enc.Utf8.parse('monkey1234567890');
    const encrypted = CryptoJS.AES.encrypt(loginParams.password, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
    const userName = CryptoJS.AES.encrypt(loginParams.username, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
    const enPassword = encrypted.ciphertext.toString(CryptoJS.enc.Base64);
    const enUserName = userName.ciphertext.toString(CryptoJS.enc.Base64);
    return {
        password: enPassword,
        username: enUserName
    }
}

